package servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import session.AppointmentDao;
import session.FootballMatchDao;
import session.PlayersDao;
import entity.Players;
import entity.User;

/**
 * Kontroler koji omogućava glasanje na MVP igrača meča.
 * @author en_e
 *
 */
public class VoteForMvp extends HttpServlet {
	private static final long serialVersionUID = -8060115207547319078L;
	
	@EJB
	protected AppointmentDao appointmentDao;
	@EJB
	protected FootballMatchDao footballMatchDao;
	@EJB
	protected PlayersDao playersDao;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			User user = (User)request.getSession().getAttribute("user");
			if (user==null) {
				response.sendRedirect(response.encodeURL("./login.jsp"));
				return;
			}
			String playerId = request.getParameter("playerId");
			String votingPlayerId = request.getParameter("votingPlayerId");
			Players player = playersDao.findById(Integer.parseInt(playerId));
			Players votingPlayer = playersDao.findById(Integer.parseInt(votingPlayerId));
			player.setMvp(player.getMvp()+1);
			votingPlayer.setVote(true);
			playersDao.merge(player);
			playersDao.merge(votingPlayer);
			getServletContext().getRequestDispatcher("/PrepareVoteMvp").forward(request, response);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
